import { defineStore } from "pinia";
import { ref } from "vue";
import { ElMessage } from "element-plus";

export const useUserStore = defineStore("user", ()=> {
  // 当前登录用户名称
  const currentUser = ref("");
  // 是否登录
  const isLogin = ref(false
    // localStorage.getItem('xdclass_user_is_login') === 'true' ? true : false
  );
  // 注册的用户信息
  const users = ref({
    account: "",
    password: "",
  });

  // 登录
  const login = (account, password) => {
    // const foundUser = users.value.find(
    //   (item) => item.account === account && item.password === password
    // );
    const foundUser = users.value.account === account && users.value.password === password;
    if (foundUser) {
      // localStorage.setItem("xdclass_user_is_login", "true");
      // localStorage.setItem("xdclass_user_current_user", foundUser.account);
      ElMessage ({
        message: "登录成功",
        type: "success",
      });
      isLogin.value = true;
      currentUser.value = foundUser.account;
      modal.switchLoginVisible();
    } else {
      ElMessage ({
        message: "账号或密码错误",
        type: "warning",
      })
    }
  }

  // 退出登录
// oxlint-disable-next-line no-unused-vars
  const logout = () => {
    // localStorage.removeItem("xdclass_user_is_login");
    // localStorage.removeItem("xdclass_user_current_user");
    isLogin.value = false;
    currentUser.value = "";
  };

  // 注册关系用户信息
  const register = (account, password) => {
  users.value = {account, password};
  // 数据缓存
  // localStorage.setItem("xdclass_vue_users", JSON.stringify(users));
  // localStorage.setItem("xdclass_user_is_login", "true");
  isLogin.value = true;
  };
  return {currentUser, isLogin, users, login, logout, register};


  // const token = ref("user");
  // return{ token };
});
